import Layout from "./pages/MyLayout";
import Chrome from "./Chrome";
import { HashRouter, useRoutes, useLocation, useNavigate } from "react-router-dom";
import NotFound from "./pages/NotFound";
import Home from "@/pages/Home";
import { Suspense, useEffect } from "react";
import Dashboard from "@/pages/Dashboard";
import DashboardContent from "@/pages/DashboardContent";
import SettingContent from "@/pages/Dashboard/SettingContent";
import DashboardOrderForm from '@/pages/DashboardOrderForm'
import DashboardNFT from "./pages/DashboardNFT/index";
import DashboardReward from "./pages/DashboardReward";
import EthBot from "./pages/bot";
import Vip from "./pages/bot/vip";
import Nft from "./pages/bot/nft";
import Transfer from "./pages/bot/transfer";
import Wallets from "./pages/bot/wallets";
import WalletsDetail from "./pages/bot/walletsDetail";
import NewWallet from "./pages/bot/newWallet";
import WalletPassword from "./pages/bot/walletPassword";
import ImportToken from "./pages/bot/importToken";
import Settings from "./pages/bot/settings";
import General from "./pages/bot/general";
import Notification from "./pages/bot/notification";
import Login from "./pages/sign/signin";
import Signup from "./pages/sign/signup";
import Reset from "./pages/sign/reset";
import Trade from "./pages/bot/trade";
// PCd端页面
import NotFoundPC from "./pages_PC/NotFound";
import LayoutPC from "./pages_PC/MyLayout";
import HomePC from "@/pages_PC/Home";
import DashboardPC from "@/pages_PC/Dashboard";
import DashboardContentPC from "@/pages_PC/DashboardContent";
import SettingContentPC from "@/pages_PC/Dashboard/SettingContent";
import DashboardOrderFormPC from '@/pages_PC/DashboardOrderForm'
import DashboardNFTPC from "./pages_PC/DashboardNFT/index";
import DashboardRewardPC from "./pages_PC/DashboardReward";
import EthBotPC from "./pages_PC/bot";
import VipPC from "./pages_PC/bot/vip";
import NftPC from "./pages_PC/bot/nft";
import TransferPC from "./pages_PC/bot/transfer";
import WalletsPC from "./pages_PC/bot/wallets";
import WalletsDetailPC from "./pages_PC/bot/walletsDetail";
import NewWalletPC from "./pages_PC/bot/newWallet";
import WalletPasswordPC from "./pages_PC/bot/walletPassword";
import ImportTokenPC from "./pages_PC/bot/importToken";
import SettingsPC from "./pages_PC/bot/settings";
import GeneralPC from "./pages_PC/bot/general";
import NotificationPC from "./pages_PC/bot/notification";
import LoginPC from "./pages_PC/sign/signin";
import SignupPC from "./pages_PC/sign/signup";
import ResetPC from "./pages_PC/sign/reset";

// 1. 准备一个路由数组 数组中定义所有的路由对应关系
const routesList = [
  {
    path: "/pc",
    element: <LayoutPC />,
    children: [
      {
        element: <HomePC />,
        index: true, // index设置为true 变成默认的二级路由
      },
    ],
  },
  {
    path: "/",
    element: <Layout />,
    children: [
      {
        element: <Home />,
        index: true, // index设置为true 变成默认的二级路由
      },
    ],
  },
  {
    path: "/dashboardPC",
    element: <DashboardPC />,
    children: [
      {
        element: <TransferPC />,
        path: "transfer",
      },
      {
        element: <WalletsPC />,
        path: "wallets",
      },
      {
        element: <WalletsDetailPC />,
        path: "walletsdetail",
      },
      {
        element: <NewWalletPC />,
        path: "newwallet",
      },
      {
        element: <WalletPasswordPC />,
        path: "walletpassword",
      },
      {
        element: <ImportTokenPC />,
        path: "importtoken",
      },
      {
        element: <SettingsPC />,
        path: "settings",
      },
      {
        element: <GeneralPC />,
        path: "general",
      },
      {
        element: <NotificationPC />,
        path: "notification",
      },
      {
        element: <DashboardContentPC />,
        path: "home",
      },
      {
        element: <DashboardOrderFormPC />,
        path: "OrderForm",
      },
      {
        element: <SettingContentPC />,
        path: "settingContent",
      },
      {
        element: <DashboardNFTPC />,
        path: "NFT",
      },
      {
        element: <Nft />,
        path: "nftbuy",
      },
      {
        element: <DashboardRewardPC />,
        path: "Reward",
      },
      {
        element: <EthBotPC />,
        path: "ethbot",
      },
      {
        element: <VipPC />,
        path: "vip",
      },
      {
        element: <NftPC />,
        path: "boardnft",
      },
    ],
  },
  {
    path: "/dashboard",
    element: <Dashboard />,
    children: [
      {
        element: <Transfer />,
        path: "transfer",
      },
      {
        element: <Wallets />,
        path: "wallets",
      },
      {
        element: <WalletsDetail />,
        path: "walletsdetail",
      },
      {
        element: <NewWallet />,
        path: "newwallet",
      },
      {
        element: <WalletPassword />,
        path: "walletpassword",
      },
      {
        element: <ImportToken />,
        path: "importtoken",
      },
      {
        element: <Settings />,
        path: "settings",
      },
      {
        element: <General />,
        path: "general",
      },
      {
        element: <Notification />,
        path: "notification",
      },
      {
        element: <DashboardContent />,
        path: "home",
      },
      {
        element: <DashboardOrderForm />,
        path: "OrderForm",
      },
      {
        element: <SettingContent />,
        path: "settingContent",
      },
      {
        element: <DashboardNFT />,
        path: "NFT",
      },
      {
        element: <Nft />,
        path: "nftbuy",
      },
      {
        element: <DashboardReward />,
        path: "Reward",
      },
      {
        element: <EthBot />,
        path: "ethbot",
      },
      {
        element: <Vip />,
        path: "vip",
      },
      {
        element: <Nft />,
        path: "boardnft",
      },
      {
        element: <Trade />,
        path: "trade",
      },
      
    ],
  },
  {
    element: <Login />,
    path: "/login",
  },
  {
    element: <Signup />,
    path: "/signup",
  },
  {
    element: <Reset />,
    path: "/reset",
  },
  // 增加n个路由对应关系
  {
    path: "*",
    element: <NotFound />,
  },
];

// // 2. 使用useRoutes方法传入routesList生成Routes组件
// function WrapperRoutes() {
//   return useRoutes(routesList);
  
// }

// import { useLocation } from "react-router-dom";

const WrapperRoutes = () => {
  const location = useLocation();
  const history = useNavigate();
  const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
  useEffect(() => {
    console.log('Location changed:', typeof chrome);
    if(location.search) {
      localStorage.setItem('codeMy', location.search.split('=')[1])
    }
    console.log(Chrome)
    if(location.pathname.includes('/dashboard')) {
      if(!isMobile && !Chrome) {
        const modifiedPath = !location.pathname.startsWith('/dashboardPC') ? location.pathname.replace('/dashboard', '/dashboardPC') : location.pathname;
        history(modifiedPath);
      }else {
        
        if(Chrome) {
          
          if(location.pathname == '/' || location.pathname == '/pc') {
            history('/dashboard/ethbot')
          }
        }else {
          const modifiedPath = location.pathname.startsWith('/dashboardPC') ? location.pathname.replace('/dashboardPC', '/dashboard') : location.pathname;
          history(modifiedPath);
        }
       
      }
    } else {
      if(Chrome) {
          
        if(location.pathname == '/' || location.pathname == '/pc') {
          localStorage.getItem('token') ? history('/dashboard/ethbot') : history('/login')
          
        }
      }else {
        if(!isMobile) {
          history('/pc');
        }else {
          history('/')
        }
      }
      
    }
  }, []);

  return useRoutes(routesList);
};

const App = () => {
  return (
    <Suspense fallback={null}>
      <HashRouter>
        <WrapperRoutes />
      </HashRouter>
    </Suspense>
  );
};

export default App;
